package de.derhess.projects.air_flashToMaxMSP
{
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.NumericStepper;
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;
	import com.bit101.components.Window;
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.tuio.connectors.UDPConnector;
	import org.tuio.osc.OSCManager;
	import org.tuio.osc.OSCMessage;
	
	[SWF(width="500", height="280", frameRate="30", backgroundColor="#FFFFFF")]
	public class as3_flashToMaxMsp extends Sprite
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var oscListener:SimpleOSCListener;
		private var oscManager:OSCManager;
		private var receiver:UDPConnector;
		private var sender:UDPConnector;
		
		//USer Interface
		private var txtAddress:InputText;
		private var numStepperSend:NumericStepper;
		private var txtTextData:InputText;
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function as3_flashToMaxMsp()
		{
			super();
			// Air stuff
			this.stage.nativeWindow.visible = true;
			NativeApplication.nativeApplication.addEventListener(Event.EXITING,handleExiting); 
			
			// Start Debugging
			MonsterDebugger.initialize(this);
			MonsterDebugger.trace(this, "Hello Air!");

			// Set up OSC 
			oscListener = new SimpleOSCListener();
			receiver = new UDPConnector("127.0.0.1",9001,true);
			sender = new UDPConnector("127.0.0.1", 9000, false);
			oscManager = new OSCManager(receiver, sender,false);
			oscManager.addMsgListener(oscListener)
			oscManager.start();
			
			//set up gui
			createSendUI();
			createReceiverUI();
		}
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		private function createSendUI():void
		{
			var sendPanel:Window = new Window(this,20,20,"Sending OSC Data");
			sendPanel.width = 150;
			sendPanel.height = 210;
			var labelSendAddress:Label = new Label(sendPanel,10,10,"OSC Address");
			this.txtAddress = new InputText(sendPanel,10,30,"/example/address",null);
			var labelSendData:Label = new Label(sendPanel,10,60,"OSC Data");
			this.numStepperSend = new NumericStepper(sendPanel,10,80,null);
			this.txtTextData = new InputText(sendPanel,10,110,"your text data",null);
			
			sendPanel.addChild( new PushButton(sendPanel,10,160,"send OSC Message",handler_click_sendOSCMessage));
		}
		
		private function createReceiverUI():void
		{
			var receiverPanel:Window = new Window(this,180,20,"Receiving OSC Data");
			receiverPanel.width = 300;
			receiverPanel.height = 210;
			
			var txtReceiver:TextArea = new TextArea(receiverPanel,10,10,"");
			txtReceiver.width = 280;
			txtReceiver.height = 170;
			txtReceiver.html = true;
			
			// Add the textArea to the oscListener for displaying the data from Max/Msp 
			oscListener.receiverText = txtReceiver;
		}
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handler_click_sendOSCMessage(event:MouseEvent):void
		{
			var myMsg:OSCMessage = new OSCMessage();
			myMsg.address = this.txtAddress.text;
			myMsg.addArgument("s", this.txtTextData.text);
			myMsg.addArgument("i",this.numStepperSend.value);
			MonsterDebugger.trace(this,"-- Message send --");
			oscManager.sendOSCPacket(myMsg);
			MonsterDebugger.inspect(myMsg);
		}
		
		private function handleExiting(event:Event):void
		{
			MonsterDebugger.trace(this,"exit Application");
			receiver.close();
			oscManager.stop();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
	}
}